﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QDragEnterEvent>
#include <QDebug>
#include <QMimeData>
#include <QProcess>
#include "zip.h"
#include "tools.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setAcceptDrops(true);
    setState(None);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    auto path = ui->label->text();
    QFileInfo info(path);
    if(info.isDir()){
        compress();
    }else if(info.isFile()){
        if(path.endsWith(".zip")){
            decompression();
        }
    }else{
        QMessageBox::critical(NULL,u8"无效路径",u8"请选择路径");
    }
}

void MainWindow::compress()
{
    auto save = QFileDialog::getSaveFileName(NULL,u8"保存名称","","ZIP (*.zip)");
    if(save.isEmpty()){
        QMessageBox::critical(NULL,u8"无效路径",u8"请输入保存路径");
        return;
    }
    auto rootPath = ui->label->text();
    setState(Runing);
    ZipResponse*res = Zip::compressAsync(rootPath,save);
    connect(res,&ZipResponse::progress,this,[=](int v,int t){
        ui->progressBar->setMaximum(t);
        ui->progressBar->setValue(v);
    });
    connect(res,&ZipResponse::finished,this,[=](bool state, const QString & msg){
        if(state){
            //QMessageBox::information(NULL,u8"压缩成功",save);
            selectZip(save);
        }else{
            QMessageBox::critical(NULL,u8"压缩失败",msg);
        }
        setState(None);
    });
}

void MainWindow::decompression()
{
    auto rootPath = ui->label->text();

    if(!QFile::exists(rootPath)){
        QMessageBox::critical(NULL,u8"错误",u8"文件不存在");
        return;
    }
    auto save = QFileDialog::getExistingDirectory(NULL,u8"选择文件夹","",  QFileDialog::ShowDirsOnly);
    if(save.isEmpty()){
        QMessageBox::critical(NULL,u8"无效路径",u8"请选择解压路径");
        return;
    }
    setState(Runing);
    ZipResponse *res= Zip::decompressionAsync(rootPath,save);
    connect(res,&ZipResponse::progress,this,[=](int v,int t){
        ui->progressBar->setMaximum(t);
        ui->progressBar->setValue(v);
    });
    connect(res,&ZipResponse::finished,this,[=](bool state, const QString & msg){
        if(state){
            QMessageBox::information(NULL,u8"解压成功",save);
        }else{
            QMessageBox::critical(NULL,u8"解压失败",msg);
        }
         setState(None);
    });
}

void MainWindow::setState(State state)
{
    QFileInfo info(ui->label->text());
    switch(state)
    {
    case None:
        ui->progressBar->hide();
        ui->pushButton->hide();
        ui->copyButton->hide();
        ui->label->setText(u8"拖拽文件夹到当前位置");
        break;
    case DropDirectory:
        ui->pushButton->show();
        ui->progressBar->hide();
        if(info.isFile() && ui->label->text().endsWith(".zip")){
            ui->pushButton->setText(u8"解压缩");
            ui->copyButton->hide();
        }else{
            ui->pushButton->setText(u8"压缩");
            ui->copyButton->show();
        }
        break;
    case Runing:
        ui->copyButton->hide();
        ui->progressBar->show();
        ui->pushButton->hide();
        break;
    }
}

void MainWindow::selectZip(const QString &path)
{
    QString strPicPath = path;
    strPicPath.replace("/", "\\");
    QProcess process;
    process.startDetached("explorer", QStringList() << QString("/select,") << QString("%1").arg(strPicPath));
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    if(event->mimeData()->hasUrls())
        event->acceptProposedAction();
    else event->ignore();
}

void MainWindow::dropEvent(QDropEvent *event)
{
    const QMimeData *mimeData = event->mimeData();
    if(mimeData->hasUrls()){
        QList<QUrl> urlList = mimeData->urls();
        QString fileName = urlList.at(0).toLocalFile();
        ui->label->setText(fileName);
        setState(DropDirectory);
    }
}

void MainWindow::on_copyButton_clicked()
{
    auto save = QFileDialog::getExistingDirectory(NULL,u8"选择文件夹","",  QFileDialog::ShowDirsOnly);
    if(save.isEmpty()){
        QMessageBox::critical(NULL,u8"无效路径",u8"请选择复制路径");
        return;
    }
    auto dir = ui->label->text();
    QDir root(dir);
    QDir saveDir(save);
    QStringList list;
    Tools::findAllfile(dir,list);
    setState(Runing);

    ui->progressBar->setMaximum(list.size());
    int progress = 0;
    for(auto path : list){
        auto relativePath = root.relativeFilePath(path);
        auto newPath = saveDir.absoluteFilePath(relativePath);
        QFileInfo info(newPath);
        if (!info.dir().exists()){
            info.dir().mkpath(info.dir().absolutePath());
        }
        QFile::copy(path,newPath);
        ui->progressBar->setValue(++progress);
        QCoreApplication::processEvents();
    }
    setState(None);
}

